﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace FACUI.BasicClass
{
    public class Device
    {
        public string DevName = "";
        public bool MainFlag = false;
        public string Note = "";
        public Device ParentDev = null;
        public string ParentDevName = "";
        public string SubType = "";
        public string Window = "";
    }
    public class StationCfg
    {
        private CfgNode staCfgRoot = null;
        public StationCfg(CfgNode staCfgNode)
        {
            if (staCfgNode != null)
            {
                this.staCfgRoot = staCfgNode;
            }
        }
 
        public int GetDeviceCount(string devTypeName)
        {
            CfgNode subNode = this.GetDevInfoCfgNode().GetSubNode(devTypeName, true).GetSubNode("设备列表");
            if (subNode != null)
            {
                return Util.ToInt(subNode["设备数目"], 0);
            }
            return 0;
        }

        public CfgNode GetDevInfoCfgNode()
        {
            if (this.staCfgRoot == null)
            {
                return null;
            }
            return this.staCfgRoot.GetSubNode("设备信息", true);
        }

        public List<CfgNode> GetDevNodeList()
        {
            CfgNode devInfoCfgNode = this.GetDevInfoCfgNode();
            if (devInfoCfgNode != null)
            {
                return CfgNode.GetSubNodes(devInfoCfgNode, devInfoCfgNode["所有设备类型"]);
            }
            return null;
        }

        public CfgNode GetDevPropCfgNode(string devTypeName, string propName, bool createIfNotExist)
        {
            return this.GetDevInfoCfgNode().GetNodeDirect(devTypeName + @"\属性列表\" + propName, createIfNotExist);
        }

        public List<CfgNode> GetDevPropCfgNodeList(CfgNode devCfgNode)
        {
            if (devCfgNode == null)
            {
                return new List<CfgNode>();
            }
            return CfgNode.GetSubNodes(devCfgNode.GetSubNode("属性列表"), devCfgNode["设备属性列表"]);
        }

        public CfgNode GetDevPropInfoCfgNode(string devType, string propName, string devID)
        {
            int deviceCount = this.GetDeviceCount(devType);
            CfgNode node = this.GetDevPropCfgNode(devType, propName, true);
            for (int i = 1; i <= deviceCount; i++)
            {
                CfgNode subNode = node.GetSubNode("设备" + i.ToString());
                if ((subNode != null) && (subNode["ID"] == devID))
                {
                    return subNode;
                }
            }
            return null;
        }

        public CfgNode GetDevTypeNode(string devTypeName)
        {
            List<CfgNode> subNodes = CfgNode.GetSubNodes(this.GetDevInfoCfgNode(), devTypeName);
            if ((subNodes != null) && (subNodes.Count > 0))
            {
                return subNodes[0];
            }
            return null;
        }

        public CfgNode GetKglCfgNode()
        {
            if (this.staCfgRoot == null)
            {
                return null;
            }
            return this.staCfgRoot.GetSubNode("开关量", true);
        }

        public CfgNode GetMapInfoCfgNode()
        {
            if (this.staCfgRoot == null)
            {
                return null;
            }
            return this.staCfgRoot.GetSubNode("站场图", true);
        }

        public CfgNode GetSettingNode()
        {
            if (this.staCfgRoot == null)
            {
                return null;
            }
            return this.staCfgRoot.GetSubNode("_Global_Setting_", true);
        }
        
        public CfgNode GetStaCfgRoot()
        {
            return this.staCfgRoot;
        }

        public string GetStationName()
        {
            return this.GetSettingNode()["车站名称"];
        }
                
        public void GetZctDevKglList(string devType, Dictionary<string, List<string>> sjkgl, Dictionary<string, List<string>> ljkgl)
        {
            CfgNode mapInfoCfgNode = this.GetMapInfoCfgNode();
            if (mapInfoCfgNode != null)
            {
                CfgNode subNode = mapInfoCfgNode.GetSubNode(devType);
                if (subNode != null)
                {
                    Dictionary<string, string> valDict = subNode.GetValDict();
                    string[] strArray = null;
                    string[] strArray2 = null;
                    List<string> list = new List<string>();
                    List<string> list2 = new List<string>();
                    foreach (string str in valDict.Values)
                    {
                        strArray = Util.SplitTrimString(str, false);
                        if ((strArray != null) && (strArray.Length >= 2))
                        {
                            for (int i = 2; i < strArray.Length; i++)
                            {
                                strArray2 = strArray[i].Split(new char[] { ';', (char)0xff1b });
                                if ((strArray2 != null) && (strArray2.Length >= 3))
                                {
                                    if (strArray2[1] == "1")
                                    {
                                        list2.Add(strArray2[0]);
                                        list.Add("");
                                    }
                                    else if (strArray2[1] == "0")
                                    {
                                        list.Add(strArray2[0]);
                                        list2.Add("");
                                    }
                                }
                            }
                            if (!sjkgl.ContainsKey(strArray[1]))
                            {
                                sjkgl.Add(strArray[1], list);
                            }
                            if (!ljkgl.ContainsKey(strArray[1]))
                            {
                                ljkgl.Add(strArray[1], list2);
                            }
                            list = new List<string>();
                            list2 = new List<string>();
                        }
                    }
                }
            }
        }
        
        public Dictionary<string, Kgl> GetZctLogicKglList()
        {
            int num = 0x186a0;
            Dictionary<string, Kgl> dictionary = new Dictionary<string, Kgl>();
            CfgNode mapInfoCfgNode = this.GetMapInfoCfgNode();
            if (mapInfoCfgNode != null)
            {
                CfgNode subNode = mapInfoCfgNode.GetSubNode("概述");
                int num2 = 0;
                if (subNode != null)
                {
                    num2 = Convert.ToInt32(subNode["逻辑开关量数目"]);
                }
                CfgNode node3 = mapInfoCfgNode.GetSubNode("逻辑开关量");
                Logic logic = null;
                for (int i = 1; i <= num2; i++)
                {
                    logic = MapData.createLogic(node3[i.ToString()]);
                    Kgl kgl = new Kgl(logic.name)
                    {
                        SubKgl1Name = logic.d1,
                        SubKgl2Name = logic.d2,
                        SubKglOp = logic.oper.ToString(),
                        id = ++num,
                        AdNo = (i + 0x8000) - 1
                    };
                    if (!dictionary.ContainsKey(logic.name))
                    {
                        dictionary.Add(logic.name, kgl);
                    }
                }
            }
            return dictionary;
        }

        public void GetZctLogicKglList(KglCollection realKglList, KglCollection logicKglList)
        {
            int maxID = -1;
            int num2 = -1;
            maxID = DeviceInfo.GetMaxID(realKglList);
            num2 = DeviceInfo.GetMaxID(logicKglList);
            if (maxID < num2)
            {
                maxID = num2;
            }
            CfgNode mapInfoCfgNode = this.GetMapInfoCfgNode();
            if (mapInfoCfgNode != null)
            {
                CfgNode subNode = mapInfoCfgNode.GetSubNode("概述");
                int num3 = 0;
                if (subNode != null)
                {
                    num3 = Convert.ToInt32(subNode["逻辑开关量数目"]);
                }
                CfgNode node3 = mapInfoCfgNode.GetSubNode("逻辑开关量");
                Logic logic = null;
                for (int i = 1; i <= num3; i++)
                {
                    logic = MapData.createLogic(node3[i.ToString()]);
                    Kgl kgl = new Kgl(logic.name)
                    {
                        SubKgl1Name = logic.d1,
                        SubKgl2Name = logic.d2,
                        SubKglOp = logic.oper.ToString(),
                        id = ++maxID
                    };
                    realKglList.Add(kgl);
                }
            }
        }

        public CfgNode GetZnfxCfgNode()
        {
            if (this.staCfgRoot == null)
            {
                return null;
            }
            return this.staCfgRoot.GetSubNode("智能分析", true);
        }

        public CfgNode GetZnfxDevInfoCfgNode()
        {
            CfgNode znfxCfgNode = this.GetZnfxCfgNode();
            if (znfxCfgNode == null)
            {
                return null;
            }
            return znfxCfgNode.GetSubNode("设备信息", true);
        }

        public bool LoadZnfxZct(string filepath)
        {
            if (!File.Exists(filepath))
            {
                return false;
            }
            return true;
        }

        public string ExtPowerType
        {
            get
            {
                return this.GetSettingNode()["外电网类型"];
            }
            set
            {
                this.GetSettingNode()["外电网类型"] = value;
            }
        }

        public bool Is480GD
        {
            get
            {
                return (this.GetSettingNode()["480轨道电路"] == "是");
            }
            set
            {
                this.GetSettingNode()["480轨道电路"] = value ? "是" : "否";
            }
        }

        public bool IsCfgExpert
        {
            get
            {
                return ((this.GetSettingNode() != null) && (this.GetSettingNode()["配置智能分析"] == "是"));
            }
            set
            {
                this.GetSettingNode()["配置智能分析"] = value ? "是" : "否";
            }
        }

        public bool IsHlHt
        {
            get
            {
                return (this.GetSettingNode()["互联互通"] == "是");
            }
            set
            {
                this.GetSettingNode()["互联互通"] = value ? "是" : "否";
            }
        }

        public bool IskzLK
        {
            get
            {
                return ((this.GetSettingNode() != null) && (this.GetSettingNode()["客专列控"] == "是"));
            }
            set
            {
                this.GetSettingNode()["客专列控"] = value ? "是" : "否";
            }
        }

        public bool IsLS
        {
            get
            {
                return (this.GetSettingNode()["微机联锁"] == "是");
            }
            set
            {
                this.GetSettingNode()["微机联锁"] = value ? "是" : "否";
            }
        }

        public string QjType
        {
            get
            {
                return this.GetSettingNode()["区间类型"];
            }
            set
            {
                this.GetSettingNode()["区间类型"] = value;
            }
        }
    }
}

